package com.microservice.wechat.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.microservice.api.wechat.dto.UserStatisticsDTO;
import com.microservice.wechat.entity.Article;
import com.microservice.api.wechat.dto.UserBindConfigDTO;
import com.microservice.wechat.mapper.ArticleMapper;
import com.microservice.wechat.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 文章服务实现类
 */
@Service
public class ArticleServiceImpl implements ArticleService {
    
    @Autowired
    private ArticleMapper articleMapper;


    @Override
    public boolean save(Article article) {
        return articleMapper.insert(article) > 0;
    }
    
    @Override
    public boolean removeById(Long id) {
        return articleMapper.deleteById(id) > 0;
    }
    
    @Override
    public boolean updateById(Article article) {
        return articleMapper.updateById(article) > 0;
    }
    
    @Override
    public Article getById(Long id) {
        return articleMapper.selectById(id);
    }
    
    @Override
    public Page<Article> page(int current, int size, Integer status, String userName) {
        Page<Article> page = new Page<>(current, size);
        // 使用关联查询方法，从 wechat_article 表获取 view_count
        return articleMapper.selectPageWithViewCount(page, status, userName);
    }
    
    @Override
    public Page<Article> listByUserId(String userId,Integer status , int current, int size) {
        Page<Article> page = new Page<>(current, size);
        // 使用关联查询方法，从 wechat_article 表获取 view_count
        return articleMapper.selectPageByUserIdWithViewCount(page, userId, status);
    }
    
    @Override
    public Page<Article> listByStatus(Integer status, int current, int size) {
        Page<Article> page = new Page<>(current, size);
        // 使用关联查询方法，从 wechat_article 表获取 view_count
        return articleMapper.selectPageByStatusWithViewCount(page, status);
    }
    
    @Override
    public UserStatisticsDTO getUserStatistics(Long userId) {
        UserStatisticsDTO statistics = articleMapper.getUserStatistics(userId);
        // 如果用户没有文章，返回默认值
        if (statistics == null) {
            statistics = new UserStatisticsDTO(userId, 0L, 0L);
        }
        return statistics;
    }

    @Override
    public List<UserBindConfigDTO> getUserBindAppId(Long userId) {
        return articleMapper.getUserBindAppId(userId);
    }
}
